package com.cqc.lite.dao;

import com.cqc.lite.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    @Select("select * from users where username=#{username}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "email",column = "email"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "PASSWORD"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "STATUS"),
            @Result(property = "roles",column = "id",javaType = List.class,
                    many = @Many(select = "com.cqc.lite.dao.RoleDao.findRolesByUserId"))
    })
    UserInfo findUserByUsername(String username);


    @Select("select * from users")
    List<UserInfo> findAll();


    @Insert("insert into users(id,email,username,password,phoneNum,status)values(#{id},#{email},#{username},#{password},#{phoneNum},#{status})")
    int save(UserInfo userInfo);

    @Select("select * from users  where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "email",column = "email"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "PASSWORD"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "STATUS"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,
                    many = @Many(select = "com.cqc.lite.dao.RoleDao.findRolesByUserId"))
    })
    UserInfo findById(String id);


    @Delete("delete from users_role where userId=#{userId}")
    void deleteUserRoleByUserId(String userId);

    @Insert("insert into users_role(userId,roleId)values(#{userId},#{roleId})")
    void insertUserRole(@Param(value = "userId") String userId, @Param(value = "roleId") String roleId);
}
